package com.auth.manager.service.sys.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth.manager.common.ex.BizException;
import com.auth.manager.mapper.sys.SysMenuMapper;
import com.auth.manager.pojo.sys.SysMenu;
import com.auth.manager.pojo.sys.TreeMenu;
import com.auth.manager.service.sys.SysMenuService;
import com.auth.manager.utils.TreeMenuUtil;
import com.auth.manager.utils.page.PageUtil;
import com.auth.manager.utils.page.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @version 1.0.0
 * @Date: 2022/4/28 20:52
 * @Author ZhuYouBin
 * @Description 系统权限
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public void createMenu(SysMenu sysMenu) {
        // 校验权限标识是否唯一
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_key", StrUtil.trim(sysMenu.getMenuKey()));
        wrapper.eq("deleted", "01");
        long count = this.count(wrapper);
        if (count > 0) {
            throw new BizException("权限标识已存在");
        }
        sysMenu.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        // 新增权限
        this.save(sysMenu);
    }

    @Override
    public void removeMenu(String mid) {
        // 查询是否存在下级权限
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", mid);
        wrapper.eq("deleted", "01");
        wrapper.last("limit 1");
        long count = this.count(wrapper);
        if (count > 0) {
            throw new BizException("请先删除下级权限");
        }
        // 查询权限
        SysMenu sysMenu = this.getById(mid);
        if (Objects.isNull(sysMenu)) {
            throw new BizException("权限不存在");
        }
        sysMenu.setDeleted("02");
        // 更新数据
        this.saveOrUpdate(sysMenu);
    }

    @Override
    public void updateMenu(SysMenu sysMenu) {
        // 查询权限
        SysMenu currentSysMenu = this.getById(sysMenu.getMid());
        if (Objects.isNull(currentSysMenu)) {
            throw new BizException("权限不存在");
        }
        // 校验权限标识是否唯一
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_key", StrUtil.trim(sysMenu.getMenuKey()));
        wrapper.ne("mid", currentSysMenu.getMid());
        wrapper.eq("deleted", "01");
        long count = this.count(wrapper);
        if (count > 0) {
            throw new BizException("权限标识已存在");
        }
        // 更新权限
        this.saveOrUpdate(sysMenu);
    }

    @Override
    public PageVo queryPage(SysMenu sysMenu, PageVo pageVo) {
        // 开启分页
        PageUtil.start(pageVo);
        List<SysMenu> list = this.list();
        return PageUtil.end(list);
    }

    @Override
    public List<TreeMenu> queryTreeMenu() {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", "01");
        // 查询菜单数据
        List<SysMenu> menuList = this.list(wrapper);
        // 构造权限树
        return TreeMenuUtil.buildTreeMenu(menuList);
    }

    @Override
    public List<SysMenu> queryRoleMenuByRid(String rid) {
        return sysMenuMapper.queryRoleMenuByRid(rid);
    }

    @Override
    public List<TreeMenu> getUserMenu(String username) {
        // 获取当前用户的菜单权限
        List<SysMenu> menuList = sysMenuMapper.queryUserMenuByUsername(username);
        // 构造权限树
        return TreeMenuUtil.buildTreeMenu(menuList);
    }

}
